package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.DrawCenterAdapter;
import flc.ast.bean.CenterBean;
import flc.ast.databinding.ActivityDrawCenterBinding;
import flc.ast.utils.Content;

public class DrawCenterActivity extends BaseAc<ActivityDrawCenterBinding> {

    private DrawCenterAdapter drawCenterAdapter;
    private List<CenterBean> listShow = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_draw_center;
    }

    @Override
    protected void initView() {
        mDataBinding.ivDrawCenterBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        drawCenterAdapter=new DrawCenterAdapter();
        mDataBinding.rvDrawList.setAdapter(drawCenterAdapter);
        StaggeredGridLayoutManager staggeredGridLayoutManager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        // 开启系统自动计算，列表机制默认开启状态。
        staggeredGridLayoutManager.setAutoMeasureEnabled(false);
//        staggeredGridLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
        mDataBinding.rvDrawList.setLayoutManager(staggeredGridLayoutManager);
        drawCenterAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        DrawDetailActivity.imgPath=listShow.get(position).getImg();
        startActivity(DrawDetailActivity.class);
    }

    @Override
    protected void initData() {
        listShow.clear();
        List<File> files = FileUtils.listFilesInDir(PathUtils.getFilesPathExternalFirst() + Content.myDraw);
        if (files.size() > 0) {
            for (File file : files) {
                listShow.add(new CenterBean(file.getPath(),true));
            }
        }
        List<File> files2 = FileUtils.listFilesInDir(PathUtils.getFilesPathExternalFirst() + Content.myPuzzle);
        if (files2.size() > 0) {
            for (File file : files2) {
                listShow.add(new CenterBean(file.getPath(),false));
            }
        }
        if (listShow.size() > 0) {
            drawCenterAdapter.setList(listShow);
            mDataBinding.rvDrawList.setVisibility(View.VISIBLE);
            mDataBinding.tvDrawCenterNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvDrawList.setVisibility(View.GONE);
            mDataBinding.tvDrawCenterNoData.setVisibility(View.VISIBLE);
        }
    }
}